
#include "StringHelper.h"
#include <algorithm>
#include <iostream>
#include "stdarg.h"
//-------------------------------------------------------------------------
// STRING

std::string& cut (std::string& s, const char ce)
{
	std::string::iterator end = std::find(s.begin(), s.end(), ce);
	return (s.assign(s.begin(), end));
}

std::string& trim(std::string& s)
{
	std::string::iterator beg = s.begin();
	while (beg != s.end() && ::isspace(*beg))
		++beg;

	std::string::reverse_iterator end = s.rbegin();
	while (end != s.rend() && ::isspace(*end))
		++end;

	return s.assign(beg, end.base());
}

std::string& trimleft(std::string& s)
{
	std::string::iterator it = s.begin();
	while (it != s.end() && ::isspace(*it))
		++it;
	return s.assign(it, s.end());
}

std::string& trimright(std::string& s)
{
	std::string::reverse_iterator it = s.rbegin();
	while (it != s.rend() && ::isspace(*it))
		++it;
	return s.assign(s.begin(), it.base());
}

std::string& toupper(std::string& s)
{
	typedef std::string::iterator SIT;
	for (SIT it = s.begin(); it != s.end(); ++it)
	{
		*it = ::toupper(*it);
	}
	return s;
}

std::string& tolower(std::string& s)
{
	typedef std::string::iterator SIT;
	for (SIT it = s.begin(); it != s.end(); ++it)
	{
		*it = ::tolower(*it);
	}
	return s;
}

std::string& format(std::string& s, const char* fmt, ...)
{
	const int MAX_LINE_LEN = 1024; // user defined
	char buf[MAX_LINE_LEN] = {0};
	va_list ap;
	va_start(ap, fmt);
	_vsnprintf(buf, MAX_LINE_LEN, fmt, ap);
	va_end(ap);
	return (s.assign(buf));
}

std::string& replace(std::string& s, const std::string& oldS, const std::string& newS)
{
	if (std::string::npos == s.find(oldS)) return (s);

	typedef std::string::size_type SST;
	SST beg =0, tmp =0;
	SST oldLen = oldS.size();
	SST newLen = newS.size();
	while (std::string::npos != (beg = (s.find(oldS, tmp))))
	{
		s.replace(beg, oldLen, newS);
		tmp = beg + newLen;
		std::cout << beg << "-" << tmp << ": " << s.c_str() << std::endl;
	}
	return (s);
}

std::string& join(const std::vector<std::string>& vs, std::string& s, const std::string& sep)
{
	if (vs.empty()) return (s);

	typedef std::vector<std::string>::const_iterator VSCIT;

	for (VSCIT it = vs.begin(); it != vs.end(); ++ it)
	{
		s += *it;
		s += sep;
	}
	return (s);
}

//	bool split(const std::string& s, std::vector<std::string >& vs, const std::string& sep)

std::vector<std::string>& split(const std::string& s, std::vector<std::string>& vs, const std::string& sep)
{
	if (s.empty()) 
		return (vs);

	if (sep.empty() || std::string::npos == s.find(sep))
	{
		vs.push_back(s);
		return (vs);
	}

	typedef std::string::size_type SST;
	SST seplen = sep.size();
	SST beg =0, end =std::string::npos;

	while ( std::string::npos != (end=s.find(sep, beg)) )
	{
		vs.push_back(s.substr(beg, end-beg));
		beg = end+seplen;
	}
	vs.push_back(s.substr(beg, s.size()-beg));

	return (vs);
}

bool getDataByTags(const std::string& s, std::vector<std::string >& tags,
				   const std::string& begTag, const std::string& endTag )
{
	if (s.empty() || begTag.empty() || endTag.empty())
		return false;

	if (std::string::npos == s.find(begTag) ||
		std::string::npos == s.find(endTag) )
	{
		return false;
	}

	typedef std::string::size_type SST;
	SST beglen = begTag.size();
	SST endlen = endTag.size();
	SST beg =0, end =0;
	tags.clear();

	while ( std::string::npos != (beg=s.find(begTag, end)) )
	{
		if( std::string::npos != (end=s.find(endTag, beg+beglen)) )
		{
			end += endlen;
			tags.push_back(s.substr(beg, end-beg));
		}
		else
			break;
	}

	return true;
}

bool getDataBetweenTags(const std::string& s, std::vector<std::string >& data,
						const std::string& begTag, const std::string& endTag )
{
	if (s.empty() || begTag.empty() || endTag.empty())
		return false;

	if (std::string::npos == s.find(begTag) ||
		std::string::npos == s.find(endTag) )
	{
		return false;
	}

	typedef std::string::size_type SST;
	SST beglen = begTag.size();
	SST endlen = endTag.size();
	SST beg =0, end =0;
	data.clear();

	while ( std::string::npos != (beg=s.find(begTag, end)) )
	{
		if( std::string::npos != (end=s.find(endTag, beg+beglen)) )
		{
			data.push_back(s.substr(beg+beglen, end-beg-beglen));
			end += endlen;
		}
		else
			break;
	}

	return true;
}